using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace ProjectSurviver
{
    public class Global : Architecture<Global>
    {
        /// <summary>
        /// 经验值
        /// </summary>
        public static BindableProperty<int> Exp = new BindableProperty<int>(0);

        /// <summary>
        /// 金币
        /// </summary>
        public static BindableProperty<int> Coin = new BindableProperty<int>(0);

        /// <summary>
        /// 等级
        /// </summary>
        public static BindableProperty<int> Level = new BindableProperty<int>(1);

        /// <summary>
        /// 伤害
        /// </summary>
        public static BindableProperty<float> SimpleAbilityDamage = new BindableProperty<float>(Config.InitSimpleSwordDamage);

        /// <summary>
        /// 攻击间隔
        /// </summary>
        public static BindableProperty<float> SimpleAbilityDuretion = new BindableProperty<float>(Config.InitSimpleSwordDuration);

        public static BindableProperty<float> CurrentScends = new BindableProperty<float>(0);

        /// <summary>
        /// 金币百分比
        /// </summary>
        public static BindableProperty<float> CoinPercent = new BindableProperty<float>(0.07f);

        /// <summary>
        /// 经验百分比
        /// </summary>
        public static BindableProperty<float> ExpPercent = new BindableProperty<float>(0.7f);

        /// <summary>
        /// 主角生命值
        /// </summary>
        public static BindableProperty<int> HP = new BindableProperty<int>(3);

        /// <summary>
        /// 最大生命值
        /// </summary>
        public static BindableProperty<int> MaxHP = new BindableProperty<int>(3);

        /// <summary>
        /// 是否解锁近身宝剑
        /// </summary>
        public static BindableProperty<bool> SimpleSwordUnlocked = new BindableProperty<bool>(false);

        /// <summary>
        /// 近身宝剑数量
        /// </summary>
        public static BindableProperty<int> SimpleSwordCount = new BindableProperty<int>(Config.InitSimpleSwordCount);

        /// <summary>
        /// 近身宝剑范围
        /// </summary>
        public static BindableProperty<float> SimpleSwordRange = new BindableProperty<float>(Config.InitSimpleSwordRange);

        /// <summary>
        /// 是否解锁飞刀
        /// </summary>
        public static BindableProperty<bool> SimpleKnifeUnlocked = new BindableProperty<bool>(false);

        /// <summary>
        /// 飞刀伤害
        /// </summary>
        public static BindableProperty<float> SimpleKnifeDamage = new BindableProperty<float>(Config.InitSimpleKnifeDamage);

        /// <summary>
        /// 飞刀间隔
        /// </summary>
        public static BindableProperty<float> SimpleKnifeDuration = new BindableProperty<float>(Config.InitSimpleKnifeDuration);

        /// <summary>
        /// 飞刀数量
        /// </summary>
        public static BindableProperty<int> simpleKnifeCount = new BindableProperty<int>(Config.InitSimpkeKnifeCount);

        /// <summary>
        /// 飞刀穿透
        /// </summary>
        public static BindableProperty<float> SimpleKnifeAttackCount = new BindableProperty<float>(1);

        /// <summary>
        /// 是否解锁转刀
        /// </summary>
        public static BindableProperty<bool> RotateSwordUnlocked = new BindableProperty<bool>(false);

        /// <summary>
        /// 转刀伤害
        /// </summary>
        public static BindableProperty<float> RotateSwordDamage = new BindableProperty<float>(Config.InitRotateSwordDamage);

        /// <summary>
        /// 转刀范围
        /// </summary>
        public static BindableProperty<float> RotateSwordRange = new BindableProperty<float>(Config.InitRotateSwordRange);

        /// <summary>
        /// 转刀速度
        /// </summary>
        public static BindableProperty<float> RotateSwordSpeed = new(Config.InitRotateSwordSpeed);

        /// <summary>
        /// 转刀数量
        /// </summary>
        public static BindableProperty<int> RotateSwordCount = new(Config.InitRotateSwordCount);

        /// <summary>
        /// 是否解锁蓝球
        /// </summary>
        public static BindableProperty<bool> BasketBallUnlocked = new(false);

        /// <summary>
        /// 蓝球伤害
        /// </summary>
        public static BindableProperty<float> BasketBallDamage = new(Config.InitBasketBallDamage);

        /// <summary>
        /// 蓝球速度
        /// </summary>
        public static BindableProperty<float> BasketBallSpeed = new(Config.InitBasketBallSpeed);

        /// <summary>
        /// 蓝球数量
        /// </summary>
        public static BindableProperty<int> BasketBallCount = new(Config.InitBasketBallCount);

        /// <summary>
        /// 是否解锁炸弹
        /// </summary>
        public static BindableProperty<bool> BoomUnlocked = new(false);

        /// <summary>
        /// 炸弹伤害
        /// </summary>
        public static BindableProperty<float> BoomDamage = new(Config.InitBoomDamage);

        /// <summary>
        /// 炸弹掉落概率
        /// </summary>
        public static BindableProperty<float> BoomPercent = new(Config.InitBoomPercent);

        /// <summary>
        /// 暴击
        /// </summary>
        public static BindableProperty<float> CriticalRate = new(Config.InitCriticalRate);

        /// <summary>
        /// 伤害率
        /// </summary>
        public static BindableProperty<float> DamageRate = new(1);

        public static BindableProperty<int> AdditionFlyThingCount = new(0);

        public static BindableProperty<float> MovementSpeedRate = new(1.0f);

        public static BindableProperty<float> CollectableArea = new(Config.InitCollectableArea);

        public static BindableProperty<float> AdditionalExpPercent = new(0);

        public static BindableProperty<bool> SuperKnife = new(false);

        public static BindableProperty<bool> SuperSword = new(false);

        public static BindableProperty<bool> SuperRotateSword = new(false);

        public static BindableProperty<bool> SuperBoom = new(false);

        public static BindableProperty<bool> SuperBasketBall = new(false);

        /// <summary>
        /// 重置数据
        /// </summary>
        public static void ResetData()
        {
            HP.Value = MaxHP.Value;
            Exp.Value = 0;
            Level.Value = 1;
            CurrentScends.Value = 0;
            SimpleSwordUnlocked.Value = false;
            SimpleAbilityDamage.Value = Config.InitSimpleSwordDamage;
            SimpleAbilityDuretion.Value = Config.InitSimpleSwordDuration;
            SimpleSwordRange.Value = Config.InitSimpleSwordRange;
            SimpleSwordCount.Value = Config.InitSimpleSwordCount;

            SimpleKnifeUnlocked.Value = false;
            SimpleKnifeDamage.Value = Config.InitSimpleKnifeDamage;
            SimpleAbilityDuretion.Value = Config.InitSimpleKnifeDuration;
            simpleKnifeCount.Value = Config.InitSimpkeKnifeCount;
            SimpleKnifeAttackCount.Value = 1;

            RotateSwordUnlocked.Value = false;
            RotateSwordDamage.Value = Config.InitRotateSwordDamage;
            RotateSwordRange.Value = Config.InitRotateSwordRange;
            RotateSwordSpeed.Value = Config.InitRotateSwordSpeed;
            RotateSwordCount.Value = Config.InitRotateSwordCount;

            BasketBallUnlocked.Value = false;
            BasketBallDamage.Value = Config.InitBasketBallDamage;
            BasketBallSpeed.Value = Config.InitBasketBallSpeed;
            BasketBallCount.Value = Config.InitBasketBallCount;

            BoomUnlocked.Value = false;
            BoomDamage.Value = Config.InitBoomDamage;
            BoomPercent.Value = Config.InitBoomPercent;

            CriticalRate.Value = Config.InitCriticalRate;
            DamageRate.Value = 1;
            AdditionFlyThingCount.Value = 0;
            MovementSpeedRate.Value = 1.0f;
            CollectableArea.Value = Config.InitCollectableArea;
            AdditionalExpPercent.Value = 0;

            SuperKnife.Value = false;
            SuperSword.Value = false;
            SuperRotateSword.Value = false;
            SuperBoom.Value = false;
            SuperBasketBall.Value = false;

            EnemyGenerator.EnemyCount.Value = 0;
            Interface.GetSystem<ExpUpgradeSystem>().ResetData();
        }

        public static int ExpToNextLevel()
        {
            return Level.Value * 5;
        }

        public static void GeneratePowerUp(GameObject gameObject, bool genTtreasureChest)
        {
            if (genTtreasureChest)
            {
                PowerUpManager.Default.TreasuerChest
                .Instantiate()
                .Position(gameObject.Position())
                .Show();
                return;
            }

            // 90% 掉落经验
            var percent = Random.Range(0, 1f);
            if (percent < ExpPercent.Value + AdditionalExpPercent.Value)
            {
                // 掉落经验
                PowerUpManager.Default.Exp.Instantiate()
                .Position(gameObject.Position())
                .Show();
                return;
            }

            percent = Random.Range(0, 1f);
            if (percent < CoinPercent.Value)
            {
                // 掉落金币
                PowerUpManager.Default.Coin.Instantiate()
                .Position(gameObject.Position())
                .Show();
                return;
            }

            percent = Random.Range(0, 1f);
            if (percent < 0.1f)
            {
                PowerUpManager.Default.HP.Instantiate()
                .Position(gameObject.Position())
                .Show();
                return;
            }

            if (BoomUnlocked.Value && !Object.FindObjectOfType<Boom>())
            {
                percent = Random.Range(0, 1f);
                if (percent < BoomPercent.Value)
                {
                    PowerUpManager.Default.Boom.Instantiate()
                    .Position(gameObject.Position())
                    .Show();
                    return;
                }
            }

            percent = Random.Range(0, 1f);
            if (percent < 0.1f)
            {
                PowerUpManager.Default.GetAllExp.Instantiate()
                .Position(gameObject.Position())
                .Show();
                return;
            }
        }

        /// <summary>
        /// 初始化 - 游戏启动
        /// </summary>
        [RuntimeInitializeOnLoadMethod]
        public static void AutoInit()
        {
            ResKit.Init();
            UIKit.Root.SetResolution(1920, 1080, 1);
            Global.MaxHP.Value = PlayerPrefs.GetInt(nameof(MaxHP), 3);
            HP.Value = MaxHP.Value;
            Coin.Value = PlayerPrefs.GetInt("coin", 0);
            ExpPercent.Value = PlayerPrefs.GetFloat(nameof(ExpPercent), 0.4f);
            CoinPercent.Value = PlayerPrefs.GetFloat(nameof(CoinPercent), 0.1f);
            Coin.Register(coin =>
            {
                PlayerPrefs.SetInt(nameof(coin), coin);
            });

            ExpPercent.Register(expPercent =>
            {
                PlayerPrefs.SetFloat(nameof(expPercent), expPercent);
            });

            CoinPercent.Register(coinPercent =>
            {
                PlayerPrefs.SetFloat(nameof(coinPercent), coinPercent);
            });

            MaxHP.Register(maxHP =>
            {
                PlayerPrefs.SetInt(nameof(MaxHP), maxHP);
            });

            var _ = Interface;
        }

        protected override void Init()
        {
            //注册系统
            this.RegisterSystem(new CoinUpgradeSystem());

            this.RegisterSystem(new SaveSystem());

            this.RegisterSystem(new ExpUpgradeSystem());
        }
    }
}